
<?php


header("Content-type:text/html;charset=utf8");//字符编码设置

error_reporting(0);
//echo "用户名:".$_POST['username']."<br>密码:".$_POST['password'];

////第一次登陆的时候，通过用户输入的信息来确认用户
if ( (($_POST['username']))!=NULL && (($_POST['password'])!=NULL)) {
    $userName = $_POST['username'];
    $password = $_POST['password'];

//从db获取用户信息
//PS：数据库连接信息改成自己的 分别为主机 数据库用户名 密码
    $conn = mysqli_connect('localhost:3306','root','124938');

    mysqli_select_db($conn,'schedule');

    mysqlI_query($conn,'SET NAMES UTF8');

    $sql = "select uid,upw from usr where uid = '$userName' and upw='$password'";
    $res = mysqli_query($conn,$sql);
    $row = mysqli_fetch_array($res);

    #判断用户是否不存在

    if (empty($row)) {
        echo '查询无用户，开始爬取数据！';
        //用户名和密码写入文件
        $fp = fopen("config.txt",'w');

        fwrite($fp,$_POST['username']);
        $word="\r\n";//换行
        fwrite($fp,$word);
        fwrite($fp,$_POST['password']);
        fclose($fp);

        //执行py脚本

        $shell="python main.py";
        $a=exec($shell." 2>&1",$resultData,$ret);
        print_r($a);

        echo '爬取成功,请刷新界面';


    }
    else if	($row['uid']!=$userName) {

        echo '不能登陆!';
        header('Location:index.html');
    }
    else if($row['upw']!=$password)
    {
        echo '不能登陆!!';
        header('Location:index.html');
    }

    else {
        //如果密码验证通过，设置一个cookies，把用户名保存在客户端
//        setcookie('username',$userName,time()+3600);//设置一个小时
        //最后跳转到登录后的欢迎页面
//        echo '登陆成功!';
//        header('Location:index1.html');//跳转到最后的欢迎页面
        echo "<script>alert('用户登陆成功！！');
//location.href='https://www.baidu.com';</script>";

        //获取两个日期之间相差周数

        function diffBetweenTwoDays ($day1, $day2)
        {
            $second1 = strtotime($day1);
            $second2 = strtotime($day2);

            if ($second1 < $second2) {
                $tmp = $second2;
                $second2 = $second1;
                $second1 = $tmp;
            }
            return 1+($second1 - $second2) / 86400 / 7;
        }
        $day1 = "2021-08-30";
        $day2 = date("Y-m-d");
        $diff = (int)diffBetweenTwoDays($day1, $day2);

        echo '查询用户存在，显示课表数据！';
        mysqli_query($conn,'SET NAMES UTF8');#设置为UTF-8
        $sql = "select * from class,contact where uid = '$userName' and class.cid=contact.cid and class.csn=contact.csn";
        $res = mysqli_query($conn,$sql);
        $row = mysqli_fetch_array($res);

        echo '运行成功';

        ?>
        <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta charset="utf-8" />
            <title>学生课表</title>
            <link rel="stylesheet" href="style.css" />
            <style>
                body {
                    background-image: url(images/background1.png);
                    background-repeat: no-repeat;
                    background-position: center top;
                    background-size: cover;
                }
            </style>
        </head>


        <body>
            <div class="header w">
                <div class="logo">
                    <img src="images/logo.png" alt="" />
                </div>

                <div class="nav">
                    <ul>
                        <li><a href="index.html" target="_self">学生课表</a></li>
                        <li><a href="classroom.html" target="_self">教室课表查询</a></li>
                        <li><a href="teacher.html" target="_self">教师课表查询</a></li>
                        <li><a href="stunotes.html" target="_self">学生笔记</a></li>
                        <li><a href="notice.html" target="_self">通知公告</a></li>
                    </ul>
                </div>
            </div>
            <?php


            if (mysqli_num_rows($res) < 1){
                echo '查询无数据！';
            }
            else{
            ?>
            <div class="header w">
                <?php

                echo "<table border=6 frame = void align = center width = 1200px height = 100px cellspacing = 0px cellpadding = 10 px>";
                echo "  <tr height = 40px align = center>
                            <td ></td><td colspan = 10 color = red><font color = \"red\"><b>课表查询系统</b></font></td>
                        </tr>";
                echo "  <tr height = 40px align = center>
                            <td ></td><td colspan = 7 color = red><font color = \"red\"><b>第 $diff 周</b></font></td>
                        </tr>";
                echo "  <tr height = 50px align = center>
                            <th width = 110px></th><th><font color = blue>周日</font></th><th><font color = blue>周一</font></th><th><font color = blue>周二</font></th><th><font color = blue>周三</font></th><th><font color = blue>周四</font></th><th><font color = blue>周五</font></th><th><font color = blue>周六</font></th>
                        </tr>";
                echo "  <tr height = 40px align = center>
                            <td ></td><td colspan = 7 color = red><font color = \"red\"><b>上午</b></font></td>
                        </tr>";
                for($i=1;$i<=2;$i++) {
                    echo "<tr height = 100px align = center>";
                    for ($j = 0; $j < 8; $j++) {
                        if ($j == 0) {
                            if($i==1)
                                echo "<td ><font color = blue><b>第一大节</b></font></td>";
                            else
                                echo "<td ><font color = blue><b>第二大节</b></font></td>";
                        }
                        else {
                            if ($j == 1) {
                                $sql = "select * from class,contact where uid = $userName and class.cid=contact.cid and class.csn=contact.csn and day='7'";
                                $res = mysqli_query($conn, $sql);
                            }
                            else {
                                $sql = "select * from class,contact where uid = $userName and class.cid=contact.cid and class.csn=contact.csn and day= $j-1 ";
                                $res = mysqli_query($conn, $sql);
                            }
                        }
                        if($j!=0 && $i==1){
                            $tag=0;
                            while($row=mysqli_fetch_array($res)){
                                    $string = $row[6];
                                    $f = explode("周",$string);
                                    $ff = explode("-",$f[0]);
                                    if($row[8]>='1' && $row[8]<='2' && $ff[0]<=$diff && $ff[1]>=$diff ){
                                        $tag=1;
                                        $a=$row[8]+$row[9]-1;
                                        echo "<td>$row[2]<br>$row[1]<br>$row[3]<br>$row[6]<br>$row[8]-$a 节<br>$row[10]<br>$row[11]<br>$row[12]</td>";
                                        break;
                                    }
                            }
                            if($tag==0) echo "<td></td>";
                        }
                        if($j!=0 && $i==2 ){
                            $tag=0;
                            while($row=mysqli_fetch_array($res)){
                                $string = $row[6];
                                $f = explode("周",$string);
                                $ff = explode("-",$f[0]);
                                if($row[8]>='3' && $row[8]<='4' && $ff[0]<=$diff && $ff[1]>=$diff ){
                                    $tag=1;
                                    $a=$row[8]+$row[9]-1;
                                    echo "<td>$row[2]<br>$row[1]<br>$row[3]<br>$row[6]<br>$row[8]-$a 节<br>$row[10]<br>$row[11]<br>$row[12]</td>";
                                    break;}
                            }
                            if($tag==0) echo "<td></td>";
                        }
                    }
                    echo "</tr>";
                    if ($i == 1) {
                        echo "  <tr height = 40px align = center>
                                    <td ></td><td colspan = 7 color = red><b>课间</b></font></td>
                                </tr>";
                    }
                }


                echo "  <tr height = 40px align = center>
                            <td ></td><td colspan = 7 color = red><font color = \"red\"><b>下午</b></font></td>
                        </tr>";
                for($i=1;$i<=2;$i++) {
                    echo "<tr height = 100px align = center>";
                    for ($j = 0; $j < 8; $j++) {
                        if ($j == 0) {
                            if($i==1)
                                echo "<td ><font color = blue><b>第三大节</b></font></td>";
                            else
                                echo "<td ><font color = blue><b>第四大节</b></font></td>";
                        }
                        else {
                            if ($j == 1) {
                                $sql = "select * from class,contact where uid = $userName and class.cid=contact.cid and class.csn=contact.csn and day='7'";
                                $res = mysqli_query($conn, $sql);
                            }
                            else {
                                $sql = "select * from class,contact where uid = $userName and class.cid=contact.cid and class.csn=contact.csn and day= $j-1 ";
                                $res = mysqli_query($conn, $sql);
                            }
                        }
                        if($j!=0 && $i==1){
                            $tag=0;
                            while($row=mysqli_fetch_array($res)){
                                $string = $row[6];
                                $f = explode("周",$string);
                                $ff = explode("-",$f[0]);
                                if($row[8]>='5' && $row[8]<='7' && $ff[0]<=$diff && $ff[1]>=$diff){
                                    $tag=1;
                                    $a=$row[8]+$row[9]-1;
                                    echo "<td>$row[2]<br>$row[1]<br>$row[3]<br>$row[6]<br>$row[8]-$a 节<br>$row[10]<br>$row[11]<br>$row[12]</td>";
                                    break;
                                }
                            }
                            if($tag==0) echo "<td></td>";
                        }
                        if($j!=0 && $i==2){
                            $tag=0;
                            while($row=mysqli_fetch_array($res)){
                                $string = $row[6];
                                $f = explode("周",$string);
                                $ff = explode("-",$f[0]);
                                if($row[8]>='8' && $row[8]<='9' && $ff[0]<=$diff && $ff[1]>=$diff){
                                    $tag=1;
                                    $a=$row[8]+$row[9]-1;
                                    echo "<td>$row[2]<br>$row[1]<br>$row[3]<br>$row[6]<br>$row[8]-$a 节<br>$row[10]<br>$row[11]<br>$row[12]</td>";
                                    break;}
                            }
                            if($tag==0) echo "<td></td>";
                        }
                    }
                    echo "</tr>";
                    if ($i == 1) {
                        echo "  <tr height = 40px align = center>
                                    <td ></td><td colspan = 7 color = red><b>课间</b></font></td>
                                </tr>";
                    }
                }


                echo "  <tr height = 40px align = center>
                            <td ></td><td colspan = 7 color = red><font color = \"red\"><b>晚上</b></font></td>
                        </tr>";

                echo "<tr height = 100px align = center>";
                for ($j = 0; $j < 8; $j++) {
                    if ($j == 0) echo "<td ><font color = blue><b>第五大节</b></font></td>";
                    else if ($j == 1) {
                        $sql = "select * from class,contact where uid = $userName and class.cid=contact.cid and class.csn=contact.csn and day='7'";
                        $res = mysqli_query($conn, $sql);
                    }
                    else {
                        $sql = "select * from class,contact where uid = $userName and class.cid=contact.cid and class.csn=contact.csn and day= $j-1 ";
                        $res = mysqli_query($conn, $sql);
                    }
                    if($j!=0){
                        $tag=0;
                        while($row=mysqli_fetch_array($res)){
                            $string = $row[6];
                            $f = explode("周",$string);
                            $ff = explode("-",$f[0]);
                            if($row[8]>='10' && $row[8]<='12' && $ff[0]<=$diff && $ff[1]>=$diff){
                                $tag=1;
                                $a=$row[8]+$row[9]-1;
                                echo "<td>$row[2]<br>$row[1]<br>$row[3]<br>$row[6]<br>$row[8]-$a 节<br>$row[10]<br>$row[11]<br>$row[12]</td>";
                                break;
                            }
                        }
                        if($tag==0) echo "<td></td>";
                    }
                }
                echo "</tr>";

                ?>


                </table>
            </div>


            <?php
            }
            echo "<script>alert('课表显示成功！！');
//        location.href='https://www.baidu.com';</script>";
            ?>
        </body>
        </html>
        <?php
    }

}
else {
    echo '登陆失败，test';
    header('Location:index.html');//跳转到失败页面
}

//if ( (($_COOKIE['username']) != null)  && (($_COOKIE['password']) != null) ) {
//    $userName = $_COOKIE['username'];
//    $password = $_COOKIE['password'];
//
//    //从db获取用户信息
//    //PS：数据库连接信息改成自己的 分别为主机 数据库用户名 密码
//    $conn = mysqli_connect('localhost:3306','root','124938','schedule');
//    $res = mysqli_query($conn,"select * from usr where uid =  '$userName' ");
//    $row = mysqli_fetch_assoc($res);
//    if ($row['upw'] == $password) {
//        //验证通过后跳转到登录后的欢迎页面
//        header('Location: https://www.baidu.com' . "?username=$userName");
//    }
//}
//else {
//    echo "<script>alert('没输入，用户名或密码错误');location.href='index.html';</script>";
//}
?>
